package com.qk.manangement.service.impl;


import ch.qos.logback.core.util.StringUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.utils.AliOssUtil;
import com.qk.common.response.PageResult;
import com.qk.common.response.UserByLabelResponse;
import com.qk.entity.pojo.User;
import com.qk.common.properties.UserPasswordProperties;
import com.qk.manangement.service.UserService;
import com.qk.manangement.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService  {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserPasswordProperties userPasswordProperties;
    @Autowired
    private AliOssUtil aliOssUtil;


    @Override
    public boolean deleteUser(Integer id) {
        return userMapper.deleteUser(id) > 0;
    }

    @Override
    public PageResult<UserByLabelResponse> getUserList(String name, String phone, Integer status, Integer dcptId, Integer page, Integer pageSize) {
        // 参数校验
        if (page == null || pageSize == null || page <= 0 || pageSize <= 0) {
            return new PageResult<>(0L, List.of());
        }

        // 设置分页参数
        PageHelper.startPage(page, pageSize);

        // 查询数据
        List<UserByLabelResponse> userList = userMapper.getUserList(name, phone, status, dcptId);

        // 构建分页结果
        PageInfo<UserByLabelResponse> pageInfo = new PageInfo<>(userList);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }



    @Override
    public boolean addUser(User user) {
        // 参数校验
        if (ObjectUtil.isNull(user)) {
            return false;
        }

        // 校验必要字段
        if (StrUtil.isBlank(user.getEmail()) ||
                StrUtil.isBlank(user.getPhone()) ||
                StrUtil.isBlank(user.getUsername()) ||
                StrUtil.isBlank(user.getName()) ||
                ObjectUtil.isNull(user.getGender()) ||
                ObjectUtil.isNull(user.getStatus())) {
            return false;
        }

        // 设置创建和更新时间
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        // 手机号脱敏处理
        user.setPhone(StrUtil.hide(user.getPhone(), 3, 7));

        // 设置默认密码
        user.setPassword(SecureUtil.md5(userPasswordProperties.getDefaultPassword() + userPasswordProperties.getSalt()));

        // 插入用户信息
        return userMapper.addUser(user) > 0;
    }

    @Override
    public User getById(Integer id) {
        // 参数校验
        if (ObjectUtil.isNull(id)) {
            return null;
        }

        // 查询用户信息
        User user = userMapper.getById(id);

        // 清除密码信息
        if (user != null) {
            user.setPassword(null);
            return user;
        }

        return null;
    }


    @Override
    public boolean updateUser(User user) {
        if (ObjectUtil.isNotNull(user.getId())
                && StrUtil.isNotBlank(user.getEmail())
                && StringUtil.notNullNorEmpty(user.getPhone())
                && StringUtil.notNullNorEmpty(user.getUsername())
                && StringUtil.notNullNorEmpty(user.getName())
                && ObjectUtil.isNotNull(user.getGender())
                && ObjectUtil.isNotNull(user.getStatus())
        ) {
            user.setPhone(StrUtil.hide(user.getPhone(), 3, 7));
            if (user.getImage() == null) {
                String dbImage = getById(user.getId()).getImage();
                aliOssUtil.delete(dbImage.substring(dbImage.indexOf("avatar")));
            }
            if (user.getImage() != null) {
                String dbImage = getById(user.getId()).getImage();
                if (!user.getImage().equals(dbImage)) {
                    aliOssUtil.delete(dbImage.substring(dbImage.indexOf("avatar")));
                }
            }
            user.setUpdateTime(LocalDateTime.now());
            return userMapper.updateUser(user) > 0;
        }
        return false;
    }

    @Override
    public List<UserByLabelResponse> getUserListByLabel(String roleLabel) {
        return userMapper.getUserListByLabel(roleLabel);
    }

    @Override
    public List<UserByLabelResponse> getUserListByDept(Integer deptId) {
        return userMapper.getUserListByDept(deptId);
    }

    @Override
    public List<UserByLabelResponse> listMy() {

        return userMapper.getUserList(null, null, null, null);
    }

    @Override
    public List<User> listByUpdateTime() {
        //mp+条件拼接生成在七天内的用户
        LambdaQueryWrapper<User> ge = new LambdaQueryWrapper<User>()
                .ge(User::getUpdateTime, LocalDateTime.now().minusDays(7));
        return list(ge);

    }


}




